//
//  GCZCarouselControls.m
//  kdxyhd
//
//  Created by Macey Chen on 16/8/10.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import "GCZCarouselControls.h"
#import "YYTimer.h"

@interface GCZCarouselControls () <UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation GCZCarouselControls

- (YYTimer *)timer {
    if (!_timer) {
        _timer = [YYTimer timerWithTimeInterval:2.2 target:self selector:@selector(carouselControlAction) repeats:YES];
    }
    return _timer;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _carouseControls = [[UIScrollView alloc] init];
        [self addSubview:_carouseControls];
        
        _containerView = [[UIView alloc] init];
        [_carouseControls addSubview:_containerView];
        
        _containerView.backgroundColor = [UIColor greenColor];
        [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_carouseControls);
            make.height.equalTo(_carouseControls);
        }];
        
        _carouseControls.bounces = NO;
        _carouseControls.pagingEnabled = YES;
//        _carouseControls.contentOffset = CGPointMake(Main_Screen_Width, 0);
        _carouseControls.showsHorizontalScrollIndicator = NO;
        _carouseControls.delegate = self;
        [_carouseControls mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        _pageControl = [[UIPageControl alloc] init];
        [self addSubview:_pageControl];
        [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@100);
            make.height.equalTo(@50);
            make.centerX.equalTo(self);
            make.bottom.equalTo(self);
        }];
        
        [_pageControl addTarget:self action:@selector(pageForRoll:) forControlEvents:(UIControlEventValueChanged)];
        
        UIImageView *imgView = [[UIImageView alloc] init];
        imgView.image = [UIImage imageNamed:@"man_banner"];
        [_containerView addSubview:imgView];
        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(_containerView).offset(-1);
            make.height.mas_equalTo(Main_Screen_Width * 0.56 + 2);
            make.width.mas_equalTo(Main_Screen_Width + 2);
        }];
        [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(imgView);
        }];
        
    }
    return self;
}

//设置滚动图以及触发的代理
- (void)setRollImageWithImageArray:(NSArray *)imageArray delegate:(id)delegate action:(SEL)action {
    NSArray *subsView = _containerView.subviews;
    for (UIView *view in subsView) {
        [view removeFromSuperview];
    }
    NSMutableArray *pictureArray = [NSMutableArray arrayWithArray:imageArray];
    [pictureArray insertObject:imageArray.firstObject atIndex:pictureArray.count];
    [pictureArray insertObject:imageArray.lastObject atIndex:0];
    _imageArray = pictureArray;
//    _carouseControls.contentSize = CGSizeMake(pictureArray.count * Main_Screen_Width, 0);
    _carouseControls.contentOffset = CGPointMake(Main_Screen_Width, 0);
    
    CZGestureImageView *lastView;
    for (int i = 0; i < pictureArray.count; i ++) {
                
        CZGestureImageView *imageView = [[CZGestureImageView alloc] initWithDeledate:delegate action:action];
        [_containerView addSubview:imageView];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(Main_Screen_Width + 2);
            make.height.mas_equalTo(Main_Screen_Width * 0.56 + 2);
            make.left.equalTo(lastView ? lastView.mas_right : _carouseControls).offset(-2);
            make.top.equalTo(_containerView).offset(- 1);
        }];
        lastView = imageView;
        imageView.image = [UIImage imageNamed:@"man_banner"];
    }
    
    [_containerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_carouseControls);
        make.height.equalTo(_carouseControls);
        make.right.equalTo(lastView);
    }];
    _pageControl.hidden = NO;
    _pageControl.numberOfPages = imageArray.count;
}

//手动pageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self pageControlAnimation];
}

//定时pageControl
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self pageControlAnimation];
}

- (void)pageControlAnimation {
    CGFloat contentSet = _carouseControls.contentOffset.x;
    NSInteger index = contentSet / Main_Screen_Width;
    if (index == _imageArray.count + 1) {
        self.pageControl.currentPage = 0;
    } else if (index == 0) {
        self.pageControl.currentPage = _imageArray.count - 1;
    } else {
        self.pageControl.currentPage = index - 1;
    }
}

//轮播图边界滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat contentSet = _carouseControls.contentOffset.x;
    
    if (contentSet >= _carouseControls.contentSize.width - Main_Screen_Width) {
        _carouseControls.contentOffset = CGPointMake(Main_Screen_Width, 0);
    } else if (contentSet <= 0) {
        _carouseControls.contentOffset = CGPointMake(_carouseControls.contentSize.width - 2 * Main_Screen_Width, 0);
    }

}

//pageControl值变化 触发方法
- (void)pageForRoll:(UIPageControl *)pageControl {
    
    NSInteger currentPage = pageControl.currentPage;
    [_carouseControls setContentOffset:CGPointMake(Main_Screen_Width * (currentPage + 1), 0) animated:YES];
    
}

- (void)carouselControlAction {
    [_carouseControls setContentOffset:CGPointMake(_carouseControls.contentOffset.x + Main_Screen_Width, 0) animated:YES];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
